package com.situ.storehouse.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.storehouse.service.AttendantService;
import com.situ.storehouse.service.AttendantServiceImpl;
import com.situ.storehouse.util.StringDbutil;


@WebServlet("/login")
public class LogininController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		//对密码进行加密之后，再与数据库中数据进行比较
		password=StringDbutil.md5(password);
		//获取前台传来请求中的验证码，判断与session中是否一致
		String captcha=request.getParameter("captcha");
		if(!((String)request.getSession().getAttribute("captcha")).equals(captcha)) {
			request.setAttribute("msg","验证码错误！");
			request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);
		}else {
			
		}
		//派service去查一下
		AttendantService service=new AttendantServiceImpl();
		Boolean check=service.check(username,password,request.getSession());
		
		if(check) {
			//如果成功了，就返回Index页面
			//定义一个保存成功状态的东东
//			request.getSession().setAttribute("currentAttendant", username);
			response.sendRedirect("/index");
		}
		else {
			request.setAttribute("msg","用户名或者密码错误！");
			request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);
			//response.sendRedirect("/login");
		}
	}

}
